package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MyMailList;
import com.prs.crm.domain.oa.MyMailListType;
import com.prs.crm.service.oa.MyMailListService;
import com.prs.crm.service.oa.MyMailListTypeService;

public class MyMailListAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	private MyMailList myMailList;
	private MyMailListService myMailListService;
	private MyMailListTypeService myMailListTypeService;
	private String birthday;
	
	@Action("listMyMailList")
	public String listMyMailList(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyMailList.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMyMailListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("selectType")
	public String selectType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyMailListType.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMyMailListTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("editMyMailList")
	public String editMyMailList(){
		return SUCCESS;
	}
	
	@Action(value = "removeMyMailList", results = {
			@Result(name = "success", type = "redirect", location = "listMyMailList"),
			@Result(name = "error", type = "chain", location = "listMyMailList") })
	public String removeMyMailList() {
		if (ids == null) {
			this.addActionError("请选择要删除的通讯录");
			return ERROR;
		}
		this.getMyMailListService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "saveMyMailList", results = {
			@Result(name = "success", type = "redirect", location = "listMyMailList"),
			@Result(name = "error", type = "chain", location = "editMyMailList") })
	public String saveMyMailList() {
		if (myMailList.getName() == null || myMailList.getName().trim().equals("")) {
			this.addActionError("请输入联系人姓名");
			return ERROR;
		}
		if(myMailList.getType() == null){
			this.addActionError("请选择所在类型");
			return ERROR;
		}
		Person person = AppContext.getAccountContext().currentUser();
		myMailList.setPerson(person);
		myMailList.setBirthday(birthday);
		this.getMyMailListService().save(myMailList);
		return SUCCESS;
	}
	
	@Action("viewMyMailList")
	public String viewMyMailList(){
		return ERROR;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public MyMailList getMyMailList() {
		return myMailList;
	}
	public void setMyMailList(MyMailList myMailList) {
		this.myMailList = myMailList;
	}
	public MyMailListService getMyMailListService() {
		return myMailListService;
	}
	public void setMyMailListService(MyMailListService myMailListService) {
		this.myMailListService = myMailListService;
	}

	public MyMailListTypeService getMyMailListTypeService() {
		return myMailListTypeService;
	}

	public void setMyMailListTypeService(MyMailListTypeService myMailListTypeService) {
		this.myMailListTypeService = myMailListTypeService;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}
